Code
library(tidyverse)
library(plotly)
library(gapminder)
using plotly to draw point,line,histogram ect
Tony Duan
July 10, 2023
# A tibble: 6 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
3 Afghanistan Asia 1962 32.0 10267083 853.
4 Afghanistan Asia 1967 34.0 11537966 836.
5 Afghanistan Asia 1972 36.1 13079460 740.
6 Afghanistan Asia 1977 38.4 14880372 786.
plot_ly(data = data002, x = ~xxx, y = ~xxx, color =~xxx)
# A tibble: 6 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 United States Americas 1997 76.8 272911760 35767.
2 Canada Americas 1997 78.6 30305843 28955.
3 Puerto Rico Americas 1997 74.9 3759430 16999.
4 Argentina Americas 1997 73.3 36203463 10967.
5 Venezuela Americas 1997 72.1 22374398 10165.
6 Chile Americas 1997 75.8 14599929 10118.
https://plotly.com/r/
---
title: "R package:[plotly]"
subtitle: "using plotly to draw point,line,histogram ect"
author: "Tony Duan"
date: "2023-07-10"
categories: [packages]
execute:
warning: false
error: false
format:
html:
toc: true
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
---

```{r}
library(tidyverse)
library(plotly)
library(gapminder)
```
```{r}
data001=gapminder
head(data001)
```
```{r}
data002= data001 %>% filter(year==1997)
```
# dot chart
plot_ly(data = data002, x = ~xxx, y = ~xxx, color =~xxx)
```{r}
plot_ly(data = data002, x = ~lifeExp, y = ~gdpPercap, color =~continent)
```
# line chart
## one data set and one variable
```{r}
data002= data001 %>% group_by(continent,year) %>% summarise(pop=sum(pop))
```
```{r}
plot_ly(data = data002, x = ~year, y = ~pop, color =~continent,mode='lines')
```
## one data set and two variable
```{r}
data002= data001 %>% group_by(year) %>% summarise(lifeExp=mean(lifeExp),gdpPercap=mean(gdpPercap)) %>% mutate(lifeExp2=lifeExp+1)
```
```{r}
plot_ly(data = data002, x = ~year, y = ~lifeExp,type = 'scatter', mode = 'lines') %>% add_trace(y = ~lifeExp2, name = 'lifeExp2', mode = 'lines+markers')
```
```{r}
library(plotly)
trace_0 <- rnorm(100, mean = 5)
trace_1 <- rnorm(100, mean = 0)
trace_2 <- rnorm(100, mean = -5)
x <- c(1:100)
data <- data.frame(x, trace_0, trace_1, trace_2)
fig <- plot_ly(data, x = ~x, y = ~trace_0, name = 'trace 0', type = 'scatter', mode = 'lines')
fig <- fig %>% add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers')
fig <- fig %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')
fig
```
# bar chart
```{r}
data002= data001 %>% filter(year==1997) %>% group_by(continent) %>% summarise(pop=sum(pop))
```
```{r}
plot_ly(data002, x = ~continent, y = ~pop, type = 'bar')
```
# histogram
```{r}
data002= data001 %>% filter(year==1997)
```
```{r}
plot_ly(data002,x = ~gdpPercap, type = "histogram",color = ~continent)
```
# box plot
```{r}
data002= data001 %>% filter(year==1997)
```
```{r}
plot_ly(data002,x=~continent,y = ~gdpPercap, type = "box")
```
```{r}
data002 %>% filter(continent=='Americas') %>% arrange(desc(gdpPercap)) %>% head()
```
# Reference
https://plotly.com/r/